/**
 * JAVACC DEMO 1.0
 */
package com.ssb.task;

import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.model.Page;
import com.apache.excp.core.impl.LoadManager;
import com.apache.excp.core.manager.ExportExecl;
import com.apache.excp.core.model.ExportVo;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.DataMap;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * description: 导出任务线程类
 *
 * @author qihan 创建时间：2017-9-14
 */
public class CashOutExcelImpTask implements Callable<Integer> {

    private Map<String, Object> params;
    private Map<String, String> dictMap;
    private String objName;
    private ExportVo vo;

    public CashOutExcelImpTask(String objName,Map<String,String> dictMap, Map<String, Object> params, ExportVo vo) {
        this.params = params;
        this.objName = objName;
        this.vo = vo;
        this.dictMap = dictMap;
    }

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     *
     * @see Callable#call()
     */
    public Integer call() throws Exception {
        int count = 1;
        try {
            boolean mark = true;
            int j = 0;
            List<DataMap> list = new ArrayList();
            params.remove("pageName");
            params.remove("formName");
            do {
//                ResultEntity entity = IusPluginUtil.getInstance().sendToIusService(pageName, formName, params);
                ResultEntity resultEntity = IusPluginUtil.getInstance().select("ssb", "list", "s_" + objName, params);

                if (null == resultEntity.getEntity()) {
                    mark = false;
                } else {
                    if (resultEntity.getEntity() instanceof Page) {
                        Page page = (Page) resultEntity.getEntity();
                        if (!Validator.isEmpty(page.getPageObjects())) {
                            list.addAll(page.getPageObjects());
                            if (j >= page.getPageCount()) {
                                mark = false;
                            } else {
                                j++;
                            }
                        } else {
                            mark = false;
                        }
                    } else if(resultEntity.getEntity() instanceof List){
                        list=(List<DataMap>) resultEntity.getEntity();
                        mark = false;
//                        System.out.println(mark);
                    }
                    else{
                        mark = false;
                    }
                }
            } while (mark);
            for (DataMap data: list ) {
                data.put("apply_status_str",dictMap.get(data.get("applyStatus")));
            }
            ExportExecl exp = LoadManager.getInstance().getExp();
            vo.setParams(objName, list);
            exp.exportData(vo);//downloadFile
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return count;
    }
}
